#include "qbcimod.h"
#include <QThread>

namespace bci {

QBciMod::QBciMod(const QString &id) : QObject() { setObjectName(id); }

void QBciMod::protectedStartSlot(::bci::QBciContext *cxt) {
  this->cxt = cxt;
  cxt->sem->acquire();
  beforeStart();
  isRunningFlag = true;
  emit started(id());
  debug("Module: " + id() + " started." + "::work on thread: " +
        QBciDebug::threadId2QStr(QThread::currentThreadId()));
  cxt->sem->release();
  afterStart();
}
void QBciMod::protectedStopSlot() {
  if (!isRunning())
    return;
  debug("Module: " + id() + " to stop.");
  cxt->sem->acquire();
  beforeStop();
  isRunningFlag = false;
  emit stopped(id());
  debug("Module: " + id() + " stopped.");
  afterStop();
  cxt->sem->release();
  cxt = nullptr;
}
void QBciMod::error(const QString &mesg) {
  cxt->logger->error(QString("[") + id() + "] " + mesg);
}
void QBciMod::warn(const QString &mesg) {
  cxt->logger->warn(QString("[") + id() + "] " + mesg);
}
void QBciMod::info(const QString &mesg) {
  cxt->logger->info(QString("[") + id() + "] " + mesg);
}
void QBciMod::debug(const QString &mesg) {
  cxt->logger->debug(QString("[") + id() + "] " + mesg);
}
} // namespace bci